//
//  GreenController.m
//  Pay
//
//  Created by steve on 15-1-13.
//  Copyright (c) 2015年 steve. All rights reserved.
//

#import "GreenController.h"
#import "AboutViewController.h"
#import "BOSSSoundWaveController.h"
#import "ASIFormDataRequest.h"
#import "commonDefines.h"
#import "JSONKit.h"
#import "AppDelegate.h"

@interface GreenController ()

@property (nonatomic, strong) BOSSSoundWaveController *soundWaveController;
@property (nonatomic, strong) AboutViewController *aboutController;
@end

@implementation GreenController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    isDeliverStated = YES;
    [self requestAsynchronouslyAction:GetInfor requestType:ActionGetInfor];
}

#pragma mark - ASIFormDataRequest -

-(void)requestAsynchronouslyAction:(NSString *)action requestType:(NSInteger)type{
    
//    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
//    if ([delegate getCurrentNetWorkStatus] == 0) {
//        
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:kMSG001 delegate:nil cancelButtonTitle:@"Ignore" otherButtonTitles:nil, nil];
//        [alert show];
//        
//        return;
//    }
    
    NSString *urlString = [IPAddress stringByAppendingString:action];
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request buildRequestHeaders];
    [request addRequestHeader:@"BasicAuthUsername" value:[[NSUserDefaults standardUserDefaults] objectForKey:GREEN_USER_ID]];
    //[request setPostValue:@"p10" forKey:@"BasicAuthUsername"];
    [request setTimeOutSeconds:10];
    request.delegate = self;
    request.tag = type;
    [request startAsynchronous];
}

#pragma mark  - ASIHTTPRequestDelegate -

-(void)requestFinished:(ASIHTTPRequest *)request{
    
    NSString *responseString = request.responseString;
    NSMutableDictionary *dic = [responseString objectFromJSONString];
    if ([[dic objectForKey:@"code"] intValue] == -1) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:[dic objectForKey:@"msg"] delegate:nil cancelButtonTitle:@"Ignore" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if ([[dic objectForKey:@"code"] intValue] == 0) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note" message:[dic objectForKey:@"msg"] delegate:nil cancelButtonTitle:@"I know" otherButtonTitles:nil, nil];
        [alert show];
        
        //将用户信息存储到本地
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:[dataDic objectForKey:@"isboss"] forKey:GREEN_USER_ISBOSS];
        [defaults setObject:[dataDic objectForKey:@"budget"] forKey:GREEN_USER_BUDGET];
        [defaults setObject:[dataDic objectForKey:@"location"] forKey:GREEN_USER_LOCATION];
        [defaults synchronize];
        
        //更新UI
        if ([[dataDic objectForKey:@"isboss"] isEqualToString:@"yes"]) {
            _isBoss = YES;
        }
        if ([[dataDic objectForKey:@"isboss"] isEqualToString:@"no"]) {
            _isBoss = NO;
            _deliver_btn.hidden = YES;
            _collect_btn.hidden = YES;
        }
    }
}
-(void)requestFailed:(ASIHTTPRequest *)request{
    NSError *error = [request error];
    NSLog(@"%@",error);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - action -

- (IBAction)onSoundWave:(id)sender {
  self.soundWaveController = [[BOSSSoundWaveController alloc] initWithNibName:@"BOSSSoundWaveController"
                                                                   bundle:nil];
    if (_isBoss) {
        self.soundWaveController.isBoss = YES;
        self.soundWaveController.isDeliver = isDeliverStated;
    }else{
        self.soundWaveController.isBoss = NO;
    }
    
  [self presentViewController:self.soundWaveController
                     animated:YES
                   completion:^{
                     
                   }];
}
- (IBAction)aboutBtnClicked:(id)sender {
    if (!_aboutController) {
        self.aboutController = [[AboutViewController alloc]initWithNibName:@"AboutViewController" bundle:nil];
    }
    [self presentViewController:self.aboutController animated:YES completion:^{
        
    }];
    
}

- (IBAction)backToMainMenu:(id)sender {
    
    [[(AppDelegate *)[[UIApplication sharedApplication] delegate] viewController].navigationController popViewControllerAnimated:YES];
}
- (IBAction)collectBtnClicked:(id)sender {
    if (!isDeliverStated) {
        return;
    }else {
        [_collect_btn.superview bringSubviewToFront:_collect_btn];
         isDeliverStated = NO;
        [self replaceButton:_collect_btn WidthConstraint:55 HeightConstraint:86];
        [_collect_btn setImage:[UIImage imageNamed:@"green_button_collect_off"] forState:UIControlStateNormal ];
        [self replaceButton:_deliver_btn WidthConstraint:47 HeightConstraint:75];
        [_deliver_btn setImage:[UIImage imageNamed:@"green_button_deliver_on"] forState:UIControlStateNormal];
    }
}

- (IBAction)deliverBtnClicked:(id)sender {
    if (isDeliverStated) {
        return;
    }else {
        isDeliverStated = YES;
        [_deliver_btn.superview bringSubviewToFront:_deliver_btn];
        [self replaceButton:_collect_btn WidthConstraint:47 HeightConstraint:75];
        [_collect_btn setImage:[UIImage imageNamed:@"green_button_collect_on"] forState:UIControlStateNormal ];
        [self replaceButton:_deliver_btn WidthConstraint:55 HeightConstraint:86];
        [_deliver_btn setImage:[UIImage imageNamed:@"green_button_deliver_off"] forState:UIControlStateNormal];
    }

}
- (void)replaceButton:(UIButton*)btn WidthConstraint:(CGFloat)Wconstant HeightConstraint:(CGFloat)Hconstant
{
    for (NSLayoutConstraint *constraint in btn.constraints) {

        if (constraint.firstItem == btn && constraint.firstAttribute == NSLayoutAttributeWidth) {
            constraint.constant = Wconstant;
        }
        if (constraint.firstItem == btn && constraint.firstAttribute == NSLayoutAttributeHeight) {
            constraint.constant = Hconstant;
        }
    }
}

@end
